package com.study.web.controller.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.study.app.domain.AppUser;
import com.study.app.domain.bo.AppUserBo;
import com.study.app.domain.bo.AppUserEmergencyBo;
import com.study.app.domain.vo.AppUserVo;
import com.study.app.service.IAppUserEmergencyService;
import com.study.app.service.IAppUserService;
import com.study.common.annotation.Log;
import com.study.common.core.controller.BaseController;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.common.enums.BusinessType;
import com.study.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * App用户信息
 *
 * @author study
 * @date 2023-08-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {

    private final IAppUserService iAppUserService;

    private final IAppUserEmergencyService iAppUserEmergencyService;

    /**
     * 查询App用户列表
     */
    @SaCheckPermission("app:user:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserVo> list(AppUserBo bo, PageQuery pageQuery) {
        TableDataInfo<AppUserVo> tableDataInfo = iAppUserService.queryPageList(bo, pageQuery);
        return tableDataInfo;
    }

    /**
     * 导出App用户列表
     */
    @SaCheckPermission("app:user:export")
    @Log(title = "App用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserBo bo, HttpServletResponse response) {
        List<AppUserVo> list = iAppUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "App用户", AppUserVo.class, response);
    }

    /**
     * 获取App用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:user:query")
    @GetMapping("/{id}")
    public R<AppUserVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(iAppUserService.queryById(id));
    }

    /**
     * 重置App用户账号密码
     *
     * @param id
     * @return
     */
    @SaCheckPermission("app:user:reset")
    @PutMapping("/reset/{id}")
    public R<Void> changeResetPassWord(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return toAjax(iAppUserService.restPassWord(id));
    }



    /**
     * 状态修改
     */
    @Log(title = "App用户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody AppUser appUser) {
        return toAjax(iAppUserService.updateAppUserStatus(appUser));
    }

    /**
     * 根据用户编码查询当前用户的紧急联系人
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserEmergency")
    public R<?> getUserEmergency(Long userId) {
        AppUserEmergencyBo bo = new AppUserEmergencyBo();
        bo.setUserId(userId);
        return R.ok(iAppUserEmergencyService.queryList(bo));
    }

    /**
     * 删除App用户
     *
     * @param ids 主键串
     */
    @Transactional
    @SaCheckPermission("app:user:remove")
    @Log(title = "App用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        for (Long id : ids) {
            AppUserVo vo = iAppUserService.queryById(id);
        }
        return toAjax(iAppUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
